<?php
   
   class Style extends Admin{
   	   
	   public function __construct(){
		parent::__construct();
	   }
	   
	   /*
	    * css列表
	    */
	   public function CssIndex(){
	   	$this->mod = "CssIndex";
		$css = DB::fetch_array("select *from `bb_css` where 1");
		$this->_T['cssList'] = $css;
		$this->template("parent");
	   } 
	   
	   /*
	    * 增加css（界面）
	    */
	   public function AddCss(){
	   	$this->mod = "CssForm";
		$this->template("parent");
	   }
	   
	   /*
	    * 增加css（处理）
	    */
	   public function CreateCss(){
	   	$css = array();
		$css['name'] = $_POST['name'];
		$css['content'] = $_POST['content'];
		$this->_updateCash("css", $css['name'], $css['content']);
		if(DB::insert("bb_css", $css)){
			$this->showmessage("css创建成功","?m=Admin/Style/CssIndex" ,2000, "返回css列表");
		}
		$this->showmessage("css创建失败","goback" ,2000, "返回");
	   }
	   
	   /*
	    * 编辑css（界面）
	    */
	   public function EditCss(){
	   	$this->mod = "CssForm";
		$this->_T['type'] = "edit";
		$cssid = intval($_GET['cssid']);
		$cssCon= DB::fetch_row("select *from `bb_css` where `cssid`='{$cssid}'");
		$this->_T['cssCon'] = $cssCon;
		$this->template("parent");
	   }
	   
	   /*
	    * 更新css（处理）
	    */
	   public function UpdateCss(){
	   	$condition = array();
	   	$condition['cssid'] = $_POST['cssid'];
	   	$css = array();
		$css['name'] = $_POST['name'];
		$css['content'] = $_POST['content'];
		$this->_updateCash("css", $css['name'], $css['content']);
		if(DB::update("bb_css", $css, $condition)){
			$this->showmessage("css更新成功","?m=Admin/Style/CssIndex" ,2000, "返回css列表");
		}
		$this->showmessage("css更新失败","goback" ,2000, "返回");
	   }
       
	   /*
	    * 删除css
	    */
	   public function DeleteCss(){
	   	$cssid = $_GET['cssid'];
	   	$condition = array();
	   	$condition['cssid'] = $cssid;
		$css = DB::fetch_row("select *from `bb_css` where `cssid`='{$cssid}'");
		$cssName = $css['name'];
		File::deleteFile("Static/css/{$cssName}.css", "delete file");
		if(DB::delete("bb_css", $condition)){
			$this->showmessage("css删除成功","?m=Admin/Style/CssIndex" ,2000, "返回css列表");
		} 
		$this->showmessage("css删除失败","goback" ,2000, "返回");
	   } 

       /*
	    * js列表
	    */
	   public function JsIndex(){
	   	$this->mod = "JsIndex";
		$js = DB::fetch_array("select *from `bb_js` where 1");
		$this->_T['jsList'] = $js;
		$this->template("parent");
	   } 
	   
	   /*
	    * 增加js（界面）
	    */
	   public function AddJs(){
	   	$this->mod = "JsForm";
		$this->template("parent");
	   }
	   
	   /*
	    * 增加js（处理）
	    */
	   public function CreateJs(){
	   	$js = array();
		$js['name'] = $_POST['name'];
		$js['content'] = $_POST['content'];
		$this->_updateCash("js", $js['name'], $js['content']);
		if(DB::insert("bb_js", $js)){
			$this->showmessage("js创建成功","?m=Admin/Style/JsIndex" ,2000, "返回js列表");
		}
		$this->showmessage("js创建失败","goback" ,2000, "返回");
	   }
	   
	   /*
	    * 编辑js（界面）
	    */
	   public function EditJs(){
	   	$this->mod = "JsForm";
		$this->_T['type'] = "edit";
		$jsid = intval($_GET['jsid']);
		$jsCon= DB::fetch_row("select *from `bb_js` where `jsid`='{$jsid}'");
		$this->_T['jsCon'] = $jsCon;
		$this->template("parent");
	   }
	   
	   /*
	    * 更新js（处理）
	    */
	   public function UpdateJs(){
	   	$condition = array();
	   	$condition['jsid'] = $_POST['jsid'];
	   	$js = array();
		$js['name'] = $_POST['name'];
		$js['content'] = $_POST['content'];
		$this->_updateCash("js", $js['name'], $js['content']);
		if(DB::update("bb_js", $js, $condition)){
			$this->showmessage("js更新成功","?m=Admin/Style/JsIndex" ,2000, "返回js列表");
		}
		$this->showmessage("js更新失败","goback" ,2000, "返回");
	   }
	   
	   /*
	    * 删除js
	    */
	   public function DeleteJs(){
	   	$jsid = $_GET['jsid'];
	   	$condition = array();
	   	$condition['jsid'] = $jsid;
		$js = DB::fetch_row("select *from `bb_js` where `jsid`='{$jsid}'");
		$jsName = $js['name'];
		File::deleteFile("Static/js/{$jsName}.js", "delete file");
		if(DB::delete("bb_js", $condition)){
			$this->showmessage("js删除成功","?m=Admin/Style/JsIndex" ,2000, "返回js列表");
		} 
		$this->showmessage("js删除失败","goback" ,2000, "返回");
	   } 
	   
	   /*
	    * 更新css、js缓存
	    */
	   private function _updateCash($type,$name,$con){
	   	$fileName = "Static/{$type}/".$name.".{$type}";
		if(File::createText($fileName, $con))
			return true;
		return false;
	   }
	   
	   /*
	    * 图片列表
	    */
	   public function ImagesIndex(){
	   	$this->mod = "ImagesIndex";
		$this->_T['images'] = DB::fetch_array("select *from `bb_file` as F left join `bb_file_path` as FP on F.`pid`=FP.`pid`
		                                     where FP.`pid`='1'");
		$this->template("parent");
	   }
	   
	   /*
	    * 图片上传（界面）
	    */ 
	   public function ImagesUpload(){
	   	$this->mod = "ImagesUpload";
		$this->template("parent");
	   }
	   
	   /*
	    * ajax函数
	    * 图片上传（处理）
	    */
	   public function ImagesUploadDeal(){
	   	$result = File::uploadFile("imgname", "image", "static");
	  	if($result==="File error")
			echo '{"code":500,"msg":"上传失败！错误原因:文件出错"}'; 
		else if($result==="File type no permit")
			echo '{"code":500,"msg":"上传失败！错误原因:该文件类型不允许上传！"}';
		else if($result==="Data base error")
			echo '{"code":200,"msg":"上传成功！数据库错误"}';
		else
			echo '{"code":500,"msg":"上传成功！"}';
	   }
	   
	   /*
	    * 删除图片
	    */
	   public function DeleteImage(){
	   	$objectid = $_GET['objectid'];
		$file = DB::fetch_row("select *from `bb_file` where `objectid`='{$objectid}'");
		File::deleteFile($file['realname'], "delete file");
		$condition = array();
		$condition['objectid'] = $objectid;
		if(DB::delete("bb_file", $condition)){
			$this->showmessage("图片删除成功","?m=Admin/Style/ImagesIndex" ,2000, "返回图片列表");
		}
		$this->showmessage("图片删除失败","goback" ,2000, "返回图片列表");	
	   }
	   
	   /*
	    * block列表
	    */
	   public function BlockList(){
	   	$this->mod = "BlockList";
		$this->template("parent");
	   }
	   
	   /*
	    * 增加block
	    */
	   public function AddBlock(){
	   	
	   }
   }



?>